package edu.hncj.seckill.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.hncj.seckill.domain.Order;
import edu.hncj.seckill.domain.Product;
import edu.hncj.seckill.response.Result;
import edu.hncj.seckill.service.OrderService;
import edu.hncj.seckill.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author anoraker
 * @Date 2021/5/3 5:21 下午
 * @Version 1.0
 */
@CrossOrigin
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @ResponseBody
    @RequestMapping(value="/getall")
    public Result getProductPage(@RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        // 常见返回集合
        Map<String,Object> map = new HashMap<String, Object>();
        // 分页
        PageHelper.startPage(page,limit);
        List<Product> products = productService.getProductPage();
        // 封装
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        map.put("code", 25);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo);
        return Result.create(map);
    }

    @ResponseBody
    @RequestMapping(value="/getnowall")
    public Result getProductPageNow(@RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        // 常见返回集合
        Map<String,Object> map = new HashMap<String, Object>();
        // 分页
        PageHelper.startPage(page,limit);
        List<Product> products = productService.getNowAll();
        // 封装
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        map.put("code", 25);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo);
        return Result.create(map);
    }

    @ResponseBody
    @RequestMapping(value="/getallafter")
    public Result getProductPageAfter(@RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        // 常见返回集合
        Map<String,Object> map = new HashMap<String, Object>();
        // 分页
        PageHelper.startPage(page,limit);
        List<Product> products = productService.getAllAfter();
        // 封装
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        map.put("code", 25);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo);
        return Result.create(map);
    }


    @ResponseBody
    @RequestMapping(value="/getone")
    public Result getProductPage(@RequestParam("id")String id){
        // 常见返回集合
        Map<String,Object> map = new HashMap<String, Object>();
        // 通过id查询商品对象
        Product product = productService.getProductById(id);
        if (product == null){
            map.put("code", 28);
            map.put("msg", "查询失败");
        }else{
            map.put("code", 29);
            map.put("product", product);
            map.put("msg", "查询成功");
        }
        return Result.create(map);
    }


    @Autowired
    OrderService orderService;

    @ResponseBody
    @RequestMapping(value="/getorder")
    public Result getOrder(@RequestParam("uid")String uid){
        // 常见返回集合
        Map<String,Object> map = new HashMap<String, Object>();
        // 通过id查询订单对象
        List<Order> orders = orderService.getOrderListByUserId(uid);
        map.put("code", 29);
        map.put("data", orders);
        map.put("msg", "查询成功");
        return Result.create(map);
    }

}
